






<!doctype html>
<html lang="zh-CN">
<head>
  <meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  
  <meta name="author" content="Chasing Dreams yu">
  
  
  
  
    <meta name="description" content="第一章:memcached 介绍1-1 memcached  是什么?
本人作为一个屌丝程序员，年少无为，卖马为生，买不起服务器，买不起域名，但是又想拥有属于自己的博客网站，那就只有通过技术来实现这一切了。memcached  是以LiveJournal  旗下 Danga  公司开发的一款软件。
free &amp; open source, high-performance, distr...">
  
  <title>memcache [ 追梦的少年丶 ]</title>
  
  
    <link rel="shortcut icon" href="/besyu/favicon.ico">
  
  
  <link rel="stylesheet" href="/besyu/css/random.css">
<link rel="stylesheet" href="/besyu/css/vegas.min.css">
<link rel="stylesheet" href="/besyu/css/highlight-railscasts.css">
<link rel="stylesheet" href="/besyu/css/jquery.fancybox.css">
<link rel="stylesheet" href="/besyu/css/iconfont/iconfont.css">
<link rel="stylesheet" href="/besyu/css/jquery.fancybox-thumbs.css">
<link rel="stylesheet" href="/besyu/css/plyr.css">
  
</head>

<body>
<div class="side-navigate hide-area">
  
    <div class="item prev">
      <a href="/besyu/2017/04/04/hello-world/">
        <div class="item-icon"></div>
      </a>
      <div class="item-title">
        Hello World
      </div>
    </div>
  
  
    <div class="item next">
      <a href="/besyu/2017/04/05/laravel工具篇/">
        <div class="item-icon"></div>
      </a>
      <div class="item-title">
        laravel工具篇
      </div>
    </div>
  
</div>
<div id="outer-container" class="hide-area">
<div id="container">
  <div id="menu-outer" class="slide-down">
    <div id="menu-inner">
      <div id="brand">
        
        <a onClick="openUserCard()">
          <img id="avatar" src="http://7te9fe.com1.z0.glb.clouddn.com/default_avatar.jpg"/>
          <div id="homelink">追梦的少年丶</div>
        </a>
      </div>
      <div id="menu-list">
        <ul>
        
        
          
            <li>
          
            <a href="/besyu/index.html">首页</a>
            
          </li>
        
          
            <li>
          
            <a href="/besyu/archives">归档</a>
            
          </li>
        
          
            <li>
          
            <a href="/besyu/tags">标签</a>
            
          </li>
        
          
            <li>
          
            <a href="/besyu/categories">分类</a>
            
          </li>
        
          
            <li>
          
            <a href="/besyu/about">关于我</a>
            
          </li>
        
        </ul>
      </div>
      <div id="show-menu">
        <button>Menu</button>
      </div>
    </div>
  </div>

  <div id="content-outer">
    <div id="content-inner">
      
      
  <article id="post">
    <h1>memcache</h1>
    <p class="page-title-sub">
      <span id = "post-title-date">撰写于 2017-04-04</span>
      
        <span id = "post-title-updated">修改于 2017-04-05</span>
      
      
      
    </p>
    
    <h1 id="第一章-memcached-介绍"><a href="#第一章-memcached-介绍" class="headerlink" title="第一章:memcached 介绍"></a>第一章:memcached 介绍</h1><h2 id="1-1-memcached-是什么"><a href="#1-1-memcached-是什么" class="headerlink" title="1-1 memcached  是什么?"></a>1-1 memcached  是什么?</h2><blockquote>
<p>本人作为一个屌丝程序员，年少无为，卖马为生，买不起服务器，买不起域名，但是又想拥有属于自己的博客网站，那就只有通过技术来实现这一切了。<br>memcached  是以LiveJournal  旗下 Danga  公司开发的一款软件。</p>
<p>free &amp; open source, high-performance, distributed memory object caching system</p>
<p>自由&amp;开放源码, 高性能 ,分布式的内存对象缓存系统</p>
</blockquote>
<p>官方网站：<a href="http://memcached.org/" target="_blank" rel="external">http://memcached.org/</a></p>
<h2 id="1-2-memcached-应用图解"><a href="#1-2-memcached-应用图解" class="headerlink" title="1-2 memcached  应用图解"></a>1-2 memcached  应用图解</h2><p>许多 Web应用都将数据保存到 RDBMS 中，应用服务器从中读取数据并在浏览器中显示。但随着数<br>据量的增大、访问的集中，就会出现 RDBMS的负担加重、数据库响应恶化、网站显示延迟等重大<br>影响。</p>
<p>这时就该 memcached 大显身手了。memcached 是高性能的分布式内存缓存服务器。一般的使用目的<br>是，通过缓存数据库查询结果，减少数据库访问次数，以提高动态Web应用的速度、提高可扩展性。</p>
<p><img src="http://on9lro4r3.bkt.clouddn.com/mem_clipboard.png" alt="image"></p>
<p>注 RDBMS:Relational Database Management System</p>
<hr>
<h1 id="第二章-memcached-基本使用"><a href="#第二章-memcached-基本使用" class="headerlink" title="第二章:memcached 基本使用"></a>第二章:memcached 基本使用</h1><h2 id="2-1-memcached-的特性"><a href="#2-1-memcached-的特性" class="headerlink" title="2-1 memcached 的特性"></a>2-1 memcached 的特性</h2><p>memcached 作为高速运行的分布式缓存服务器，具有以下的特点</p>
<ul>
<li>协议简单</li>
<li>基于 libevent的事件处理</li>
<li>内置内存存储方式</li>
<li>memcached 不互相通信的分布式</li>
</ul>
<p><strong>协议简单</strong></p>
<p>memcached 的服务器客户端通信并不使用复杂的 XML等格式，而使用简单的基于文本行的协议。<br>因此，通过 telnet也能在memcached 上保存数据、取得数据。</p>
<p>举个栗子</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">$ telnet localhost 11211</div><div class="line">Trying 127.0.0.1...</div></pre></td></tr></table></figure>
<table>
<thead>
<tr>
<th>命令</th>
<th>介绍</th>
</tr>
</thead>
<tbody>
<tr>
<td>set foo 0 0 3</td>
<td>（保存命令）</td>
</tr>
<tr>
<td>bar</td>
<td>（数据）</td>
</tr>
<tr>
<td>STORED</td>
<td>（结果）</td>
</tr>
<tr>
<td>get foo</td>
<td>（取得命令）</td>
</tr>
<tr>
<td>VALUE foo 0 3</td>
<td>（数据）</td>
</tr>
<tr>
<td>bar</td>
<td>（数据）</td>
</tr>
</tbody>
</table>
<h2 id="2-2-window-下-memcache-安装"><a href="#2-2-window-下-memcache-安装" class="headerlink" title="2-2 window 下 memcache 安装"></a>2-2 window 下 memcache 安装</h2><p>打开cmd，用cd命令 切换至memcache目录下 </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">## 一步完成安装</div><div class="line">memcached.exe -d   install </div><div class="line"></div><div class="line">## 卸载</div><div class="line">memcached.exe -d   uninstall</div></pre></td></tr></table></figure>
<h2 id="2-3-window-下-memcache-启动"><a href="#2-3-window-下-memcache-启动" class="headerlink" title="2-3 window 下 memcache 启动"></a>2-3 window 下 memcache 启动</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">## 普通启动</div><div class="line">memcached -d start   </div><div class="line"></div><div class="line">## 监听端口启动</div><div class="line">memcached -p 11212</div><div class="line"></div><div class="line">## 启动 memcache 端口11211 给他分配32M内存并且打印操作信息</div><div class="line">memcache.exe -p 11211 -m 32 -vv</div></pre></td></tr></table></figure>
<table>
<thead>
<tr>
<th>命令</th>
<th>介绍</th>
</tr>
</thead>
<tbody>
<tr>
<td>-p <num></num></td>
<td>监听的TCP端口 (缺省: 11211)</td>
</tr>
<tr>
<td>-d</td>
<td>以守护进程方式运行Memcached</td>
</tr>
<tr>
<td>-u <username></username></td>
<td>运行Memcached的账户，非root用户</td>
</tr>
<tr>
<td>-m <num></num></td>
<td>最大的内存使用, 单位是MB，缺省是 64 MB</td>
<td></td>
</tr>
<tr>
<td>-c <num></num></td>
<td>软连接数量, 缺省是 1024</td>
</tr>
<tr>
<td>-v</td>
<td>输出警告和错误信息</td>
</tr>
<tr>
<td>-vv</td>
<td>打印客户端的请求和返回信息</td>
</tr>
<tr>
<td>-h</td>
<td>打印帮助信息</td>
</tr>
<tr>
<td>-i</td>
<td>打印memcached和libevent的版权信息</td>
</tr>
</tbody>
</table>
<h2 id="2-4-window-下-memcache-简单操作"><a href="#2-4-window-下-memcache-简单操作" class="headerlink" title="2-4 window 下 memcache 简单操作"></a>2-4 window 下 memcache 简单操作</h2><p><strong>1. telnet操作memcache</strong></p>
<p>确保本地已经安装 telnet </p>
<p>安装步骤：</p>
<p>控制面板==&gt;程序==&gt;启用或关闭windows功能==&gt;Telnet客户端</p>
<p>打开 cmd ,使用telnet 连接至memcache</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">telnet  127.0.0.1  11211</div></pre></td></tr></table></figure>
<p><strong>2. 数据存储命令</strong></p>
<p>a. set 命令<br>    格式：set key flag expiretime bytes</p>
<pre><code>说明：有则改无则增
</code></pre><p>举个栗子<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">set username 0 0 4</div></pre></td></tr></table></figure></p>
<p>注：最后一位是字节，那么在输入存如内容时，大小必须是2个字节，否则存储不成功。</p>
<p>b. add 命令<br>    格式：add key flag expiretime bytes</p>
<pre><code>说明：add与set一致，区别是不能添加已经存在一个key为username的数据，否则不成功。
</code></pre><p>举个栗子<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">add username 0 0 4</div></pre></td></tr></table></figure></p>
<p><strong>数据修改命令</strong></p>
<p>a. replace 命令<br>    格式: replace key flag expiretime bytes</p>
<pre><code>说明：replace与set一致，区别是只有数据存在时才能进行数据更新，如果replace一个不存在的key的数据，则replace不成功。
</code></pre><p>举个栗子<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">replace username 0 0 4</div><div class="line">```    </div><div class="line">    </div><div class="line">    </div><div class="line">**数据读取命令**</div><div class="line"></div><div class="line">a. get命令 </div><div class="line">    格式： get key</div><div class="line">    </div><div class="line">    说明：get空格key 可以获取指定key的数据。多个key可以用空格隔开</div><div class="line"></div><div class="line">举个栗子</div></pre></td></tr></table></figure></p>
<p>get username<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"></div><div class="line">**数据删除命令**</div><div class="line">a. delete命令</div><div class="line">    格式：delete key </div><div class="line">    </div><div class="line">    说明：删除已存在的键值和不存在的记录可以返回不同的结果。</div><div class="line">举个栗子</div></pre></td></tr></table></figure></p>
<p>delete username<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"></div><div class="line">b. 清空所有数据 flush_all [time]</div><div class="line"></div><div class="line">举个栗子</div></pre></td></tr></table></figure></p>
<p>flush_all[60]<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">注： time参数是指是所有缓存失效,并在time秒内限制使用删除的key</div><div class="line"></div><div class="line">**其他命令**</div><div class="line"></div><div class="line">a. 增减: incr/decr key value</div><div class="line">举个栗子</div></pre></td></tr></table></figure></p>
<p>incr age 1<br>decr age 1<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div></pre></td><td class="code"><pre><div class="line"></div><div class="line">b. 服务器状态命令 stats</div><div class="line"></div><div class="line">名称 | 解释</div><div class="line">---|---</div><div class="line">STAT pid 22459 | 进程ID </div><div class="line">STAT uptime 1027046 |服务器运行秒数  </div><div class="line">STAT time 1273043062| 服务器当前unix时间戳 </div><div class="line">STAT version 1.4.4 | 服务器版本  </div><div class="line">STAT pointer_size 64|操作系统字大小(64位) </div><div class="line">STAT total_connections 82|曾打开的连接总数  </div><div class="line">STAT cmd_get 54|执行get命令总数  </div><div class="line">STAT cmd_set 34|执行set命令总数 </div><div class="line">STAT cmd_flush |指向flush_all命令总数  </div><div class="line">STAT get_hits 9|get命中次数 </div><div class="line">STAT get_misses 45|get未命中次数</div><div class="line">STAT delete_misses 5|delete未命中次数</div><div class="line">STAT delete_hits 1|delete命中次数</div><div class="line">STAT accepting_conns 1 | 目前接受的链接数 </div><div class="line">STAT bytes 0 | 存储item字节数</div><div class="line">STAT curr_items 0 | item个数</div><div class="line">STAT evictions 0 | 为获取空间删除item的总数</div><div class="line"></div><div class="line"></div><div class="line">---</div><div class="line">**名词解释**</div><div class="line"></div><div class="line">key是什么?  （add/replace/set 的第一个参数）</div><div class="line">- key是缓存名  memcached的一个重要特点就是key-value缓存</div><div class="line">即键值对缓存.  每个缓存有一个独特的名字和存储空间. </div><div class="line">- key是操作数据的唯一标识</div><div class="line">- key可以250个字节以内，不能有空格和控制字节</div><div class="line"></div><div class="line"></div><div class="line">flag有什么用?  （add/replace/set 的第二个参数）</div><div class="line"></div><div class="line">flag是&quot;标志&quot;的意思,可以用此参数来标志内容的类型</div><div class="line">对于字符串,直接存5个字符即可, 对于array,则需要序列化.</div></pre></td></tr></table></figure></p>
<p>Q：取出数据时,又如何处理呢?</p>
<p>A：字符串,取回直接用, 数组,则需要反序列化成数组.<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"></div><div class="line"></div><div class="line">提示:在memcache中可以忽略flag参数memcache自动识别</div><div class="line"></div><div class="line">expire是啥（add/replace/set 的第三个参数）</div><div class="line"></div><div class="line">说明：过期时间 可以为时间戳 也可以是秒数 默认是0 永久有效（其实并不是永久有效,下面会给大家说）</div></pre></td></tr></table></figure></p>
<p>Q：expire以什么为单位?</p>
<p>A：秒为单位<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"></div><div class="line"></div></pre></td></tr></table></figure></p>
<p>Q：expire的秒数代表什么?</p>
<p>A：如果expire&lt;=30<em>24</em>60*60,则代表自当前时间的偏移，即有效期在 time()+expire以内.<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"></div><div class="line">如果expire &gt; 30 * 24 * 60 * 60 ,则直接代表时间戳.</div><div class="line"></div><div class="line">即,在1970年+expire秒以内有效.</div><div class="line"></div><div class="line"># 第三章:Linux 平台下搭建 memcached 服务</div><div class="line"></div><div class="line">## 3-1 编译安装 Memcached 服务</div><div class="line"></div><div class="line">在 linux 编译,需要 gcc,make,cmake,autoconf,libtool 等工具,所以请先装.</div><div class="line">【Linux 系统需联网】,命令安装</div></pre></td></tr></table></figure></p>
<h1 id="yum-install-gcc-make-cmake-autoconf-libtool"><a href="#yum-install-gcc-make-cmake-autoconf-libtool" class="headerlink" title="yum install gcc make cmake autoconf libtool"></a>yum install gcc make cmake autoconf libtool</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"></div><div class="line">memcached 依赖于 libevent 库,因此我们需要先安装 libevent.</div><div class="line"></div><div class="line">分别到 libevent.org 和 memcached.org 下载最新的 stable 版本(稳定版).</div><div class="line"></div><div class="line">libevent 官网：http://libevent.org</div><div class="line"></div><div class="line">[下载地址](https://github.com/libevent/libevent/releases/download/release-2.1.8-stable/libevent-2.1.8-stable.tar.gz)(Github地址)</div><div class="line"></div><div class="line">编译安装 libevent</div></pre></td></tr></table></figure>
<p>wget <a href="https://github.com/libevent/libevent/releases/download/release-2.1.8-stable/libevent-2.1.8-stable.tar.gz" target="_blank" rel="external">https://github.com/libevent/libevent/releases/download/release-2.1.8-stable/libevent-2.1.8-stable.tar.gz</a></p>
<p>tar zxvf libevent-2.0.21-stable.tar.gz<br>cd libevent-2.0.21-stable<br>./configure –prefix=/usr/local/libevent<br>make &amp;&amp; make install<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"></div><div class="line"></div><div class="line">再编译安装 memcached</div><div class="line"></div><div class="line">Memcached 官网：http://memcached.org/</div><div class="line"></div><div class="line">编译 memcached 时要指定 libevent 的路径.</div></pre></td></tr></table></figure></p>
<p>wget <a href="http://www.memcached.org/files/memcached-1.4.36.tar.gz" target="_blank" rel="external">http://www.memcached.org/files/memcached-1.4.36.tar.gz</a></p>
<p>tar zxvf memcached-1.4.36.tar.gz<br>./configure –prefix=/usr/local/memcached –with-libevent=/usr/local/libevent/<br>make &amp;&amp; make install</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"></div><div class="line">memcached 的启动</div><div class="line">从终端输入以下命令，启动 memcached。</div></pre></td></tr></table></figure>
<h1 id="usr-local-memcached-bin-memcached-p-11211-m-64m-u-root-vv"><a href="#usr-local-memcached-bin-memcached-p-11211-m-64m-u-root-vv" class="headerlink" title="/usr/local/memcached/bin/memcached -p 11211 -m 64m -u root -vv"></a>/usr/local/memcached/bin/memcached -p 11211 -m 64m -u root -vv</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"></div><div class="line">作为 daemon 后台启动时，只需 `-d`</div></pre></td></tr></table></figure>
<h1 id="usr-local-memcached-bin-memcached-p-11211-m-64m-u-root-d"><a href="#usr-local-memcached-bin-memcached-p-11211-m-64m-u-root-d" class="headerlink" title="/usr/local/memcached/bin/memcached -p 11211 -m 64m -u root -d"></a>/usr/local/memcached/bin/memcached -p 11211 -m 64m -u root -d</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"></div><div class="line">这里使用的 memcached 启动选项的内容如下。</div><div class="line"></div><div class="line">选项 | 说明</div><div class="line">---|---</div><div class="line">-p | 使用的 TCP端口。默认为 11211</div><div class="line">-m | 最大内存大小。默认为64M</div><div class="line">-vv| 用 very vrebose 模式启动，调试信息和错误输出到控制台</div><div class="line">-d | 作为 daemon 在后台启动</div><div class="line"></div><div class="line">上面四个是常用的启动选项，其他还有很多，通过</div></pre></td></tr></table></figure>
<h1 id="usr-local-memcached-bin-memcached-h"><a href="#usr-local-memcached-bin-memcached-h" class="headerlink" title="/usr/local/memcached/bin/memcached -h"></a>/usr/local/memcached/bin/memcached -h</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">命令可以显示。许多选项可以改变 memcached 的各种行为，推荐读一读。</div><div class="line"></div><div class="line"></div><div class="line">**常出现问题一**: </div><div class="line"></div><div class="line">在虚拟机下练习编译,一个容易碰到的问题---虚拟机的时间不对,</div><div class="line">导致的 gcc 编译过程中,检测时间通不过,一直处于编译过程.</div><div class="line"></div><div class="line">解决方法:</div></pre></td></tr></table></figure>
<h1 id="date-s-‘yyyy-mm-dd-hh-mm-ss’"><a href="#date-s-‘yyyy-mm-dd-hh-mm-ss’" class="headerlink" title="date -s ‘yyyy-mm-dd hh:mm:ss’"></a>date -s ‘yyyy-mm-dd hh:mm:ss’</h1><h1 id="clock-w-把时间写入-cmos"><a href="#clock-w-把时间写入-cmos" class="headerlink" title="clock -w  # 把时间写入 cmos"></a>clock -w  # 把时间写入 cmos</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"></div><div class="line">**常出现问题二**:</div></pre></td></tr></table></figure>
<p>[root@iZ2ze2mkw2q2od0ctv8jnfZ memcached]# bin/memcached<br>can’t run as root without the -u switch<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">解释：没有指定- u开关不能作为根用户运行</div><div class="line"></div><div class="line"></div><div class="line">## 3-2 Memcache 服务的安全</div><div class="line"></div><div class="line">此时本地客户端Telnet或php程序就可以连接至线上Memcache，没有任何的验证过程，这样如果服务器是直接暴露在互联网上的话是比较危险，轻则数据泄露被其他无关人员查看，重则服务器被入侵。</div><div class="line"></div><div class="line"></div><div class="line">解决方案</div><div class="line"></div><div class="line">**1. 内网访问**</div><div class="line"></div><div class="line">把两台服务器之间的访问是内网形态的，一般是Web服务器跟Memcache服务器之间。。普遍的服务器都是有两块网卡，一块指向互联网，一块指向内网，那么就让Web服务器通过内网的网卡来访问Memcache服务器，</div><div class="line"></div><div class="line">Memcache的服务器上启动的时候就监听内网的IP地址和端口。</div></pre></td></tr></table></figure></p>
<h1 id="memcached-d-m-1024-u-root-l-127-0-0-1-p-11211-c-1024-P-tmp-memcached-pid"><a href="#memcached-d-m-1024-u-root-l-127-0-0-1-p-11211-c-1024-P-tmp-memcached-pid" class="headerlink" title="memcached -d -m 1024 -u root -l 127.0.0.1 -p 11211 -c 1024 -P /tmp/memcached.pid"></a>memcached -d -m 1024 -u root -l 127.0.0.1 -p 11211 -c 1024 -P /tmp/memcached.pid</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">启动监听内网的127.0.0.1的ip的11211端口，占用1024MB内存，并且允许最大1024个并发连接</div><div class="line"></div><div class="line">**2. 设置防火墙**</div><div class="line"></div><div class="line">如若必可避免暴漏在外网，防火墙是简单有效的方式，那么可以考虑使用防火墙来过滤非法访问。</div><div class="line"></div><div class="line">一般我们在Linux下可以使用iptables或者FreeBSD下的ipfw来指定一些规则防止一些非法的访问。</div></pre></td></tr></table></figure>
<h1 id="iptables-F"><a href="#iptables-F" class="headerlink" title="iptables -F"></a>iptables -F</h1><h1 id="iptables-P-INPUT-DROP"><a href="#iptables-P-INPUT-DROP" class="headerlink" title="iptables -P INPUT DROP"></a>iptables -P INPUT DROP</h1><h1 id="iptables-A-INPUT-p-tcp-s-192-168-0-2-–dport-11211-j-ACCEPT"><a href="#iptables-A-INPUT-p-tcp-s-192-168-0-2-–dport-11211-j-ACCEPT" class="headerlink" title="iptables -A INPUT -p tcp -s 192.168.0.2 –dport 11211 -j ACCEPT"></a>iptables -A INPUT -p tcp -s 192.168.0.2 –dport 11211 -j ACCEPT</h1><h1 id="iptables-A-INPUT-p-udp-s-192-168-0-2-–dport-11211-j-ACCEPT"><a href="#iptables-A-INPUT-p-udp-s-192-168-0-2-–dport-11211-j-ACCEPT" class="headerlink" title="iptables -A INPUT -p udp -s 192.168.0.2 –dport 11211 -j ACCEPT"></a>iptables -A INPUT -p udp -s 192.168.0.2 –dport 11211 -j ACCEPT</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">上面的iptables规则就是只允许192.168.0.2这台Web服务器对Memcache服务器的访问，能够有效的阻止一些非法访问，相应的也可以增加一些其他的规则来加强安全性，这个可以根据自己的需要来做。</div><div class="line"></div><div class="line"></div><div class="line">## 3-3 Linux 上编译 memcached 扩展</div><div class="line"></div><div class="line"></div><div class="line">### 3-3-1 编译安装 libmemcached </div><div class="line"></div><div class="line">安装基础依赖库</div></pre></td></tr></table></figure>
<p>yum -y install gcc gcc-c++ libstdc++-devel<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"></div><div class="line">libmemcached 官网 `http://libmemcached.org/libMemcached.html`</div><div class="line"></div><div class="line"></div><div class="line">编译安装</div></pre></td></tr></table></figure></p>
<p>wget <a href="https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz" target="_blank" rel="external">https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz</a></p>
<p>tar libmemcached-1.0.18.tar.gz<br>tar zxvf libmemcached-1.0.18.tar.gz<br>cd libmemcached-1.0.18<br>./configure –prefix=/usr/local/libmemcached<br>make &amp;&amp; make install<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"></div><div class="line">### 3-3-2 编译安装 memcached </div><div class="line"></div><div class="line">下载 Memcached 扩展包</div><div class="line">下载地址：http://pecl.php.net/package/memcached</div></pre></td></tr></table></figure></p>
<p>wget <a href="http://pecl.php.net/get/memcached-3.0.3.tgz" target="_blank" rel="external">http://pecl.php.net/get/memcached-3.0.3.tgz</a><br>tar memcached-3.0.3.tgz<br>tar zxvf memcached-3.0.3.tgz<br>cd memcached-3.0.3</p>
<p>/usr/local/php/bin/phpize </p>
<p>./configure –with-php-config=/usr/local/php/bin/php-config –with-libmemcached-dir=/usr/local/libmemcached/ –disable-memcached-sasl</p>
<p>make &amp;&amp; make install</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"></div><div class="line"></div><div class="line">如果看到如下结果，那么恭喜您。</div></pre></td></tr></table></figure>
<p>[root@iZ2ze2mkw2q2od0ctv8jnfZ memcached-3.0.3]# make install<br>Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20151012/</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"></div><div class="line">打开自己环境中的 `php.ini`，加入如下</div></pre></td></tr></table></figure>
<h1 id="vim-usr-local-php-lib-php-ini"><a href="#vim-usr-local-php-lib-php-ini" class="headerlink" title="vim /usr/local/php/lib/php.ini"></a>vim /usr/local/php/lib/php.ini</h1><p>extension=memcached.so<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">打印 `phpinfo()`，`ctrl+f` 搜索 memcached，看到如下</div><div class="line"></div><div class="line">![image](http://on9lro4r3.bkt.clouddn.com/memcached.jpg)</div><div class="line"></div><div class="line">至此，你的`PHP`已支持`memcached`收工。</div><div class="line"></div><div class="line">**错误分析**</div></pre></td></tr></table></figure></p>
<p>configure: error: no, sasl.h is not available. Run configure with –disable-memcached-sasl to disable this check</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">按提示所说`--disable-memcached-sasl`禁用该选项即可。</div><div class="line"></div><div class="line">## 3-3 Memcache 存储SESSION</div><div class="line"></div><div class="line">**方法I:** 在 php.ini 中全局设置</div></pre></td></tr></table></figure>
<p>session.save_handler = memcache<br>session.save_path = “tcp://127.0.0.1:11211”<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"></div><div class="line">**方法II：** 应用中动态更改配置：</div></pre></td></tr></table></figure></p>
<p>ini_set(“session.save_handler”, “memcache”);<br>ini_set(“session.save_path”,”tcp://127.0.0.1:11211”);<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"></div><div class="line">**方法III：** 项目目录下的 .htaccess</div></pre></td></tr></table></figure></p>
<p>php_value session.save_handler “memcache”<br>php_value session.save_path “tcp://127.0.0.1:11211”<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"></div><div class="line"></div><div class="line"># 6-1 Memcache 的监控</div><div class="line"></div><div class="line"></div><div class="line">## 6-1-1 memcache.php</div><div class="line"></div><div class="line">http://pecl.php.net/package/memcache</div><div class="line"></div><div class="line">从 memcache-3.0.8.tgz 包中找到 memcache.php，拷贝至你的项目下,进行如下配置</div></pre></td></tr></table></figure></p>
<p>define(‘ADMIN_USERNAME’,’memcache’);     // Admin Username<br>define(‘ADMIN_PASSWORD’,’password’);      // Admin Password<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"></div><div class="line"></div></pre></td></tr></table></figure></p>
<p>$MEMCACHE_SERVERS[] = ‘mymemcache-server1:11211’; // add more as an array<br>$MEMCACHE_SERVERS[] = ‘mymemcache-server2:11211’; // add more as an array<br>```</p>
<p>不用多说，相信你懂的，完成以上配置后，访问（本宝宝的访问地址）：<a href="http://localhost/memcache.php" target="_blank" rel="external">http://localhost/memcache.php</a></p>
<p><img src="http://on9lro4r3.bkt.clouddn.com/memcached_2.jpg" alt="image"></p>
<h2 id="6-1-2-memcache-top"><a href="#6-1-2-memcache-top" class="headerlink" title="6-1-2 memcache-top"></a>6-1-2 memcache-top</h2><blockquote>
<p>这个工具memcache-top是一个perl脚本，可以运行在term下。它能够像top一样显示各个memcached节点的状态变化，其中包括系统管理员最关心的几个指数，例如：缓存命中率、内存、使用率、读写QPS等。</p>
</blockquote>
<p>运行前要确保memcache-top脚本具有可执行权限<br>chmod u+x *.sh    ，然后使用./执行即可。</p>
<p>memcache-top 比较重要的几个参数包括：</p>
<ul>
<li><p>–commands: 显示GETS/SETS命令的调用次数</p>
</li>
<li><p>–sleep: 刷新间隔，默认为3秒</p>
</li>
<li><p>–lifetime: 显示自memcached启动以来的累计统计值，默认关闭，即仅显示瞬时速率</p>
</li>
</ul>
<p><img src="http://on9lro4r3.bkt.clouddn.com/memcache-top.png" alt="image"></p>
<h2 id="6-1-3-memadmin"><a href="#6-1-3-memadmin" class="headerlink" title="6-1-3 memadmin"></a>6-1-3 memadmin</h2><blockquote>
<p>基于 PHP5 &amp; JQuery 的 Memcached 管理监控工具</p>
</blockquote>
<p>官方网址：<br><a href="http://www.junopen.com/memadmin/" target="_blank" rel="external">http://www.junopen.com/memadmin/</a></p>
<hr>

  </article>
  <div class="random-toc-area">
  <button class="btn-hide-toc btn-hide-toc-show" style="display: none" onclick="TOCToggle()">显示目录</button>
  <button class="btn-hide-toc btn-hide-toc-hide" onclick="TOCToggle()">隐藏目录</button>
  <div class="random-toc">
    <h2>目录</h2>
    <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#第一章-memcached-介绍"><span class="toc-text">第一章:memcached 介绍</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-1-memcached-是什么"><span class="toc-text">1-1 memcached  是什么?</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-2-memcached-应用图解"><span class="toc-text">1-2 memcached  应用图解</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#第二章-memcached-基本使用"><span class="toc-text">第二章:memcached 基本使用</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#2-1-memcached-的特性"><span class="toc-text">2-1 memcached 的特性</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-2-window-下-memcache-安装"><span class="toc-text">2-2 window 下 memcache 安装</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-3-window-下-memcache-启动"><span class="toc-text">2-3 window 下 memcache 启动</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-4-window-下-memcache-简单操作"><span class="toc-text">2-4 window 下 memcache 简单操作</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#yum-install-gcc-make-cmake-autoconf-libtool"><span class="toc-text">yum install gcc make cmake autoconf libtool</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#usr-local-memcached-bin-memcached-p-11211-m-64m-u-root-vv"><span class="toc-text">/usr/local/memcached/bin/memcached -p 11211 -m 64m -u root -vv</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#usr-local-memcached-bin-memcached-p-11211-m-64m-u-root-d"><span class="toc-text">/usr/local/memcached/bin/memcached -p 11211 -m 64m -u root -d</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#usr-local-memcached-bin-memcached-h"><span class="toc-text">/usr/local/memcached/bin/memcached -h</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#date-s-‘yyyy-mm-dd-hh-mm-ss’"><span class="toc-text">date -s ‘yyyy-mm-dd hh:mm:ss’</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#clock-w-把时间写入-cmos"><span class="toc-text">clock -w  # 把时间写入 cmos</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#memcached-d-m-1024-u-root-l-127-0-0-1-p-11211-c-1024-P-tmp-memcached-pid"><span class="toc-text">memcached -d -m 1024 -u root -l 127.0.0.1 -p 11211 -c 1024 -P /tmp/memcached.pid</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#iptables-F"><span class="toc-text">iptables -F</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#iptables-P-INPUT-DROP"><span class="toc-text">iptables -P INPUT DROP</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#iptables-A-INPUT-p-tcp-s-192-168-0-2-–dport-11211-j-ACCEPT"><span class="toc-text">iptables -A INPUT -p tcp -s 192.168.0.2 –dport 11211 -j ACCEPT</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#iptables-A-INPUT-p-udp-s-192-168-0-2-–dport-11211-j-ACCEPT"><span class="toc-text">iptables -A INPUT -p udp -s 192.168.0.2 –dport 11211 -j ACCEPT</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#vim-usr-local-php-lib-php-ini"><span class="toc-text">vim /usr/local/php/lib/php.ini</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#6-1-2-memcache-top"><span class="toc-text">6-1-2 memcache-top</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-1-3-memadmin"><span class="toc-text">6-1-3 memadmin</span></a></li></ol></li></ol>
  </div>
</div>

  
<nav id="pagination">
  
    <a href="/besyu/2017/04/04/hello-world/" class="prev">&larr; 上一篇 Hello World</a>
  

  

  
    <a href="/besyu/2017/04/05/laravel工具篇/" class="next">下一篇 laravel工具篇 &rarr;</a>
  
</nav>

  <!-- JiaThis Button BEGIN -->

<!-- JiaThis Button END -->


      
      
      
    </div>
  </div>

  <div id="bottom-outer">
    <div id="bottom-inner">
      Site by Chasing Dreams yu using
      <a href="http://hexo.io">Hexo</a> & <a href="https://github.com/stiekel/hexo-theme-random">Random</a>
      <br>
      
    </div>
  </div>
</div>

</div>



<div id="user-card">
  <div class="center-field">
    <img class="avatar" src="http://7te9fe.com1.z0.glb.clouddn.com/default_avatar.jpg">
    <p id="description"></p>
    <ul class="social-icon">
  
  
    <li>
      <a href="https://github.com/stiekel/hexo-theme-random">
        
          <i class="icon iconfont github">&#xe606;</i>
        
      </a>
    </li>
  
    <li>
      <a href="https://coding.net/u/Stiekel">
        
          <i class="icon iconfont coding">&#xe607;</i>
        
      </a>
    </li>
  
    <li>
      <a href="https://twitter.com/SidCN">
        
          <i class="icon iconfont twitter">&#xe600;</i>
        
      </a>
    </li>
  
    <li>
      <a href="http://weibo.com/sidcn">
        
          <i class="icon iconfont weibo">&#xe602;</i>
        
      </a>
    </li>
  
    <li>
      <a href="http://www.douban.com/people/Stiekel/">
        
          <i class="icon iconfont douban">&#xe60f;</i>
        
      </a>
    </li>
  
</ul>
  </div>
</div>


<div id="btn-view">Hide</div>

<script>
// is trigger analytics / tongji script
var isIgnoreHost = false;

if(window && window.location && window.location.host) {
  isIgnoreHost = ["localhost","127.0.0.1"].some(function(address){
    return 0 === window.location.host.indexOf(address);
  });
}

var isTriggerAnalytics = !( true && isIgnoreHost );

</script>




  
  
    <script src="/besyu/js/jquery-2.2.3.min.js"></script>
  
    <script src="/besyu/js/vegas.min.js"></script>
  
    <script src="/besyu/js/random.js"></script>
  
    <script src="/besyu/js/highlight.pack.js"></script>
  
    <script src="/besyu/js/jquery.mousewheel.pack.js"></script>
  
    <script src="/besyu/js/jquery.fancybox.pack.js"></script>
  
    <script src="/besyu/js/jquery.fancybox-thumbs.js"></script>
  
    <script src="/besyu/js/plyr.js"></script>
  

<script>

  // fancybox
  var backgroundImages = [];
  
  $('#post').each(function(i){
    $(this).find('img').each(function(){
      if ($(this).parent().hasClass('fancybox') || $(this).parent().hasClass('fancybox-thumb')) return;
      var alt = this.alt || this.title;
      if (alt) $(this).after('<span class="caption">' + alt + '</span>');
      $(this).wrap('<a href="' + this.src + '" title="' + alt + '" class="fancybox"></a>');
    });
    $(this).find('.fancybox').each(function(){
      $(this).attr('rel', 'post' + i);
    });
  });
  $(".fancybox").fancybox();

var vegasConfig = {"preload­Image":true,"transition":["slideLeft2","slideRight2","flash2"],"timer":true,"delay":5000,"shuffle":true,"count":28};
var unsplashConfig = {"gravity":"north"};
// is show background images
var turnoffBackgroundImage = false;



  turnoffBackgroundImage = true;


var backgroundColor = "34495E";

$(".fancybox-thumb").fancybox({
  prevEffect: 'none',
  nextEffect: 'none',
  helpers: {
    title: {
      type: 'outside'
    },
    thumbs: {
      width: 50,
      height: 50
    }
  }
});

// show video with plyr
$(".video-container iframe").each(function(i){
  var url = $(this).attr('src');
  var id = url.split('/').pop();
  var plyrContainer = document.createElement('div');
  plyrContainer.className = 'plyr';
  var plyrElement = document.createElement('div');
  plyrElement.dataset.videoId = id;
  switch(true) {
    case url.search('youtube.com') >= 0:
      plyrElement.dataset.type = 'youtube';
      break;
    case url.search('vimeo.com') >= 0:
      plyrElement.dataset.type = 'vimeo';
      break;
    default:
      return;
  };
  plyrContainer.appendChild(plyrElement);
  $(this).parent().html(plyrContainer);
});
plyr.setup('.plyr', {iconUrl: '/besyu/css/sprite.svg'});
</script>
</body>
</html>

